package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.constant.ConstantBasic;
import com.mf.utils.UtilIdGenerator;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import com.mf.entity.dto.DtoRive;
import com.mf.entity.req.search.SearchReqRive;
import com.mf.mapper.MapperRive;
import com.mf.service.ServiceRive;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;


@Service
public class ImplServiceRive extends ServiceImpl<MapperRive, DtoRive> implements ServiceRive {

    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoRive> fillParams(SearchReqRive params) {
        LambdaQueryWrapper<DtoRive> wrapper = loadQw();
        if (StringUtils.isNotEmpty(params.getKey())) {
            wrapper.and(w -> {
                w.like(DtoRive::getCustomerName, params.getKey())
                        .or()
                        .like(DtoRive::getActualWorkFlag, params.getKey())
                        .or()
                        .like(DtoRive::getWorkFlag, params.getKey())
                        .or()
                        .like(DtoRive::getWorkCode, params.getKey());
            });
        }

        wrapper.eq(ObjectUtils.isNotEmpty(params.getCustomerId()), DtoRive::getCustomerId, params.getCustomerId())
                .eq(ObjectUtils.isNotEmpty(params.getActualWorkId()), DtoRive::getActualWorkId,
                        params.getActualWorkId())
                .eq(ObjectUtils.isNotEmpty(params.getStatus()), DtoRive::getStatus, params.getStatus());

        wrapper.orderByDesc(DtoRive::getCreatedTime);
        return wrapper;
    }

    /**
     * 修改来料单关联状态
     */
    @Override
    @Transactional
    public void updateRelevancyStatus(Integer id, Integer relevancy) {
        final DtoRive dtoRive = new DtoRive();
        dtoRive.setId(id);
        dtoRive.setRelevanceStatus(relevancy);
        this.updateById(dtoRive);
    }

    /**
     * 保存
     */
    @Override
    public DtoRive saveRive(DtoRive dtoRive) {
        LambdaQueryWrapper<DtoRive> wrapper = loadQw();
        LocalDate date = LocalDate.now();
        wrapper.between(DtoRive::getCreatedTime, date.atStartOfDay(), date.atTime(LocalTime.MAX))
                .orderByDesc(DtoRive::getCreatedTime)
                .last("limit 1");
        DtoRive rive = getOne(wrapper);
        if (ObjectUtils.isNotEmpty(rive)) {
            dtoRive.setWorkCode(UtilIdGenerator.generateId(ConstantBasic.RIVE_CODE_PREFIX,
                    ConstantBasic.CODE_NUMBER_LENGTH, rive.getWorkCode()));
        } else {
            dtoRive.setWorkCode(UtilIdGenerator.generateId(ConstantBasic.RIVE_CODE_PREFIX,
                    ConstantBasic.CODE_NUMBER_LENGTH, ConstantBasic.START_NUMBER));
        }

        this.save(dtoRive);

        return dtoRive;
    }
}